<?php

namespace app\admin\controller\general;

use app\admin\model\Admin;
use app\common\controller\Backend;
use fast\Http;
use fast\Random;
use think\Env;
use think\Session;
use think\Validate;

/**
 * 个人配置
 *
 * @icon fa fa-user
 */
class Profile extends Backend
{

    protected $searchFields = 'id,title';

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            $this->model = model('AdminLog');
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->where($where)
                ->where('admin_id', $this->auth->id)
                ->order($sort, $order)
                ->paginate($limit);

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 更新个人信息
     */
    public function update()
    {
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post("row/a");
            $params = array_filter(array_intersect_key(
                $params,
                array_flip(array('email', 'nickname', 'password', 'avatar', 'mobile', 'smscode'))
            ));
            if (!Validate::is($params['email'], "email")) {
                $this->error(__("Please input correct email"));
            }
            if (isset($params['password'])) {
                if (!Validate::is($params['password'], "/^[\S]{6,30}$/")) {
                    $this->error(__("Please input correct password"));
                }
                $params['salt'] = Random::alnum();
                $params['password'] = md5(md5($params['password']) . $params['salt']);
            }
            $exist = Admin::where('email', $params['email'])->where('id', '<>', $this->auth->id)->find();
            if ($exist) {
                $this->error(__("Email already exists"));
            }
            if (!isset($params['mobile'])) { // 当手机号没有输入时，为了保存为空
                $params['mobile'] = '';
            }
            if ($params) {
                $admin = Admin::get($this->auth->id);
                // 修改手机号码
                if (empty($params['mobile'])) {

                } else {
                    $oldMobile= $admin->mobile;
                    if ($oldMobile == $params['mobile']) {

                    } else {
                        if (!isset($params['smscode'])) {
                            $this->error('修改手机号码时需要输入验证码');
                        }
                        $domain = Env::get('chaotian.domain');
                        $checkSmsCodeUrl = $domain . "sys/sms/admin/verify/".$params['mobile']."?smsCode=".$params['smscode'];
                        $res = Http::get($checkSmsCodeUrl);
                        $res = json_decode($res, true);

                        if (isset($res['code']) and 20000 == $res['code']) {
                        } else {
                            $this->error($res['msg']);
                        }
                    }
                }
//                unset($params['smscode']); //去掉，要不会引起保存错误
                $admin->allowField(['email', 'nickname', 'password', 'avatar', 'mobile'])->save($params);
                //因为个人资料面板读取的Session显示，修改自己资料后同时更新Session
                Session::set("admin", $admin->toArray());
                $this->success();
            }
            $this->error();
        }
        return;
    }
}
